<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Category
 *
 * Allows to manage category informations
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Category extends User_Controller {
	
	function Category( ) {
		parent::User_Controller( );
		
		$this->load->helper( 'form' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Category' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the category model
		$this->load->model( 'category_model' );
		$this->load->model( 'website_model' );
		
		// Load the article model
		$this->lang->module_load( 'article', 'category' );
		
		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'categroy' ), 'article/user/category' );
		
		log_message( 'debug', 'conlabz CMS : Category class loaded' );
	}	
	
	/**
	 * View Categories
	 *
	 * @access public
	 */
	function index( ) {
		//@todo: should be better
		$lang = ( $this->lang->lang() == 'de' ) ? 1 : 2 ;
		//Get website information
		$website = " AND language_id = '".$lang."' AND website_id = " . $this->session->userdata( 'website_id' );
		
		// Get Categroy Infomation
		$data['categories'] = '';
		$data['categories'] = $this->category_model->buildCategoryUnorderedList( 'category', $website );

		// Display Page
		$data['header'] = $this->lang->line( 'category_overview' );
		$data['page']   = $this->config->item('backendpro_template_user') . "/view_category";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'article';
		$this->load->view( $this->_container, $data );
	}
	
	/**
	 * Get Category Details
	 *
	 * Load category detail values from the submited form
	 *
	 * @access private
	 * @return array
	 */
	function _get_category_details(){
		$data['id']          = $this->input->post( 'id' );
		$data['name']        = $this->input->post( 'name' );
		$data['url_title']   = $this->input->post( 'url_title' );
		$data['description'] = $this->input->post( 'description' );
		$data['category_id'] = $this->input->post( 'category_id' );
		$data['language_id'] = $this->input->post( 'language_id' );

		$data['website_id'] = $this->session->userdata( 'website_id' );
		
		return $data;
	}
	
	/**
	 * Display Category Form
	 *
	 * @access public
	 * @param integer $id Category ID
	 */
	function form( $id = NULL, $translation = 1 ) {
		// VALIDATION FIELDS
		$fields['id']          = "ID";
		$fields['name']        = $this->lang->line( 'category_name'        );
		$fields['url_title']   = $this->lang->line( 'category_url_title'   );
		$fields['website_id']  = $this->lang->line( 'category_website'     );
		$fields['description'] = $this->lang->line( 'category_description' );
		$fields['category_id'] = $this->lang->line( 'category_category'    );
		$fields['language_id'] = $this->lang->line( 'category_language'    );
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create article rules (make sure no-one has the same short name)
			$rules['name']        = "trim|required";
			$rules['url_title']   = "trim|required";
			$rules['description'] = "trim";
		} else {
			// Use edit article rules (make sure no-one other than the current user has the same short name)
			$rules['name']        = "trim|required";
			$rules['url_title']   = "trim|required";
			$rules['description'] = "trim";
		}
		
		// Setup form default values
		if( !is_null( $id ) && $translation == 1 && !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$category = $this->category_model->getCategories( 'category.id = ' . $id . ' AND category.language_id = 1' );
			$category = $category->row_array( );
		
			$this->validation->set_default_value( $category );
		} elseif( !is_null( $id ) && $translation != 1 && !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$category = $this->category_model->getCategories( 'category.id = ' . $id . ' AND category.language_id = ' . $translation  );
			$category = $category->row_array( );
		
			$this->validation->set_default_value( $category );
		} elseif( is_null( $id ) && !$this->input->post( 'submit' ) ) {
			// Create form, first load
			$this->validation->set_default_value( 'category_id', $this->preference->item( 'category_category' ) );
		} elseif( $this->input->post( 'submit' ) ) {
			//@todo: validation needs to be done!
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			$where = " AND website_id = " . $this->session->userdata( 'website_id' ). " AND language_id = " . $translation;
			// Construct Category dropdown
			$this->load->model( 'category_model' );
			$data['category'] = $this->category_model->buildCategoryDropdown( 'category', 'id', $where );
			// Construct Website dropdown
			$this->load->model( 'website_model' );
			$data['website'] = $this->website_model->buildWebsiteDropdown( );
			// Construct Language dropdown
			$this->load->model( 'language_model' );
			$data['language'] = $this->language_model->buildLanguageDropdown( );
			$data['default_lang'] = $translation;
			
			// Display form
			$this->validation->output_errors();
			$data['fckeditor'] = array(
              	'name'        => 'description',
              	'id'          => 'description',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH . 'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300'
    		);
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_category' ) : $this->lang->line( 'edit_category' ) );
			$this->bep_site->set_crumb( $data['header'],'article/user/category/form/'.$id );
			$data['page'] = $this->config->item( 'backendpro_template_user' ) . "/form_category";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'article';
			$this->load->view( $this->_container, $data );
		} else {
			// Save form
			if( is_null( $id ) && $translation == 1 ) {
				// CREATE
				// Fetch form values
				$category = $this->_get_category_details( );

				$this->db->trans_start( );
				$this->category_model->insert( $category );
				
				if( $this->db->trans_status( ) === TRUE ){
					$this->db->trans_commit();
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $category['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line('backendpro_action_failed'), $this->lang->line('userlib_create_user') ) );
				}
				redirect('article/user/category');
			} elseif( !is_null( $id ) && $translation != 1 ) {
				// CREATE
				// Fetch form values
				$category = $this->_get_category_details( );

				$this->db->trans_start( );
				$this->category_model->insert( $category );
				
				if( $this->db->trans_status( ) === TRUE ){
					$this->db->trans_commit();
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $category['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line('backendpro_action_failed'), $this->lang->line('userlib_create_user') ) );
				}
				redirect('article/user/category');
			} else {
				// SAVE
				$category = $this->_get_category_details();
				
				$this->db->trans_start();
				$this->category_model->update( $category, $category['id'] );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'category_saved' ), $category['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				redirect('article/user/category');
			}
		}
	}
	
	/**
	 * Delete Categories
	 *
	 * @access public
	 */
	function delete( ) {
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ){
			redirect( 'article/user/article', 'location' );
		}

		foreach( $selected as $category ) {
			$this->category_model->delete( $category );
		}

		flashMsg( 'success', $this->lang->line( 'category_deleted' ) );
		redirect( 'article/user/category', 'location' );
	}
		
}
/* End of file category.php */
/* Location: ./modules/article/controllers/user/category.php */